<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/26
 * Time: 16:40
 */

namespace App\Game;

use App\Models\GameCategory;
use Carbon\Carbon;
use App\Game\Traits\VgTrait;
use App\Models\OddsHandicap;
use App\Models\Def\MainGamePlat;

class vg extends Game
{
    use VgTrait;

    private $platName = 'vg';



    public function __construct()
    {
        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'plat_type' => 'card',
            'status' => 1
        ];
    }

    public function getLength()
    {
        return $this->length;
    }

    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }


    /**
     * gameapi
     * 2018/9/24 14:16
     * Administrator
     * curlCreateMember
     * 创建用户
     * @param $accountUserName
     * @param $password
     * @param $request
     * @return array
     */
    public function curlCreateMember($accountUserName, $password, $request)
    {
        $params = [
            'action' => 'create',
            'username' => $accountUserName,
            'channel' => $this->channel,
            'agent' => ''
        ];

        $output = $this->vgRequest($params);

        if (array_key_exists('errcode', $output)) {
            if ($output['errcode'] == 0) {
                return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => '创建用户失败，错误原因：' . $output['errtext']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }

    /**
     * gameapi
     * 2018/9/24 14:15
     * Administrator
     * curlGetBalance
     * 查询余额
     * @param $accountUserName
     * @return array
     */
    public function curlGetBalance($accountUserName)
    {
        $params = [
            'action' => 'balance',
            'username' => $accountUserName,
            'channel' => $this->channel,
        ];

        $output = $this->vgRequest($params);

        if (array_key_exists('errcode', $output)) {
            if ($output['errcode'] == 0 && $output['errtext'] == 'success') {
                return ['success' => true, 'data' => ['balance' => $output['result']], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => '查询余额失败，错误原因：' . $output['errtext']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }

    }


    //转入游戏
    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $params = [
            'action' => 'deposit',
            'username' => $accountUserName,
            'channel' => $this->channel,
            'amount' => $price,
            'serial' => $transferid,
        ];

        $output = $this->vgRequest($params);

        if (array_key_exists('errcode', $output)) {
            if ($output['errcode'] == 0) {
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败，错误原因：' . $output['errtext']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    //转出游戏
    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $params = [
            'action' => 'withdraw',
            'username' => $accountUserName,
            'channel' => $this->channel,
            'amount' => $price,
            'serial' => $transferid,
        ];

        $output = $this->vgRequest($params);

        if (array_key_exists('errcode', $output)) {
            if ($output['errcode'] == 0) {
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败，错误原因：' . $output['errtext']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    //查询订单状态
    public function curlCheckTransfer($request)
    {
        $params = [
            'action' => 'transRecord',
            'username' => $request->accountUserName,
            'serial' => $request->transferid,
        ];

        $output = $this->vgRequest($params);

        if (array_key_exists('errcode', $output)) {
            if ($output['errcode'] == 0) {
                return ['success' => true, 'data' => ['transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '检查转账失败，错误原因：' . $output['errtext']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    //踢线
    public function curlKick($accountUserName)
    {
        return ['success' => false, 'data' => [], 'message' => 'VG平台没有踢线功能'];
    }

    /**
     * gameapi
     * 2018/9/24 16:35
     * Administrator
     * curlJoinGame
     * 进入游戏
     * @param $request
     * @return array
     */
    public function curlJoinGame($request)
    {
        $params = [
            'action' => 'loginWithChannel',
            'username' => $request->accountUserName,
            'channel' => $this->channel,
            'gametype' => $request->gamecode,
            'gameversion' => '1',
            'orientation' => '',
        ];

        $output = $this->vgRequest($params);

        if (array_key_exists('errcode', $output)) {
            if ($output['errcode'] == 0) {
                return ['success' => true, 'data' => ['url' => $output['result']], 'message' => '成功'];
            } else {
                return ['success' => false, 'data' => [], 'message' => '进入游戏失败，错误原因：' . $output['errtext']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }

    }


    /**
     * gameapi
     * 2018/9/24 16:35
     * Administrator
     * curlJoinMobileGame
     * 手机端进入游戏
     * @param $request
     * @return array
     */
    public function curlJoinMobileGame($request)
    {
        $params = [
            'action' => 'loginWithChannel',
            'username' => $request->accountUserName,
            'channel' => $this->channel,
            'gametype' => $request->gamecode,
            'gameversion' => '2',
            'orientation' => '',
        ];

        $output = $this->vgRequest($params);

        if (array_key_exists('errcode', $output)) {
            if ($output['errcode'] == 0) {
                return ['success' => true, 'data' => ['url' => $output['result']], 'message' => '成功'];
            } else {
                return ['success' => false, 'data' => [], 'message' => '进入游戏失败，错误原因：' . $output['errtext']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }
    }


    //游戏抓单
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        $data = [];
        $maxId = $this->getMaxId();

        $params = [
            'channel' => $this->channel,
            'agent' => '',
            'id' => $maxId,
        ];

        $output = $this->vgRecordRequest($params);

        if (array_key_exists('state', $output)) {
            if ($output['state'] == 0) {
                $result = $output['value'];
                if (count($result) >= 1) {
                    $data = array_merge($data, $result);
                }

            }
        }

        return $data;
    }
}